import { NoticeService } from './../../../providers/notice-service';
import { Component } from '@angular/core';
import { NavController, NavParams, ToastController, LoadingController } from 'ionic-angular';

import { Api } from '../../../providers/api-service';
import { UrlService } from '../../../providers/url-service';
import { LoginPage } from '../login';

/**
 * 找回密码
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@Component({
  selector: 'page-password-reset',
  templateUrl: 'password-reset.html',
})
export class PasswordResetPage {

  mobile: string = '';
  vcode: string = '';
  password: string = '';
  password2: string = '';
  disabled: boolean = true;
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public toastCtrl: ToastController,
    public loadingCtrl: LoadingController,
    public noticeService: NoticeService,
    public api: Api,
    public url: UrlService
  ) {
  }

  ionViewDidLoad() {
    //console.log('ionViewDidLoad PasswordResetPage');
    clearInterval(this.countDownTimer);
  }


  /**
   * 获取验证码
   */
  getVerifyCode() {

    // if (this.mobile == "" || this.mobile == null) {
    //   this.noticeService.showNoticeByToast(1, "手机号码不能为空");
    // }else{
    //   this.countDown();
    // }

    //判断手机是否可以注册
    if (this.mobile == null || this.mobile == "") {
      this.api.showNoticeByToast(1, "手机号不能为空");
      return false;
    } else if (!(/^1[34578]\d{9}$/.test(this.mobile))) {
      this.api.showNoticeByToast(1, "请输入正确的格式手机号码！");
      return false;
    } else {
      // this.url.CheckPhoneNumber(this.mobile)
      //   .then(data => {
      //     //console.log(data);
      //     if (data.Code == 2) {

      //     } else {
      //       this.api.showNoticeByToast(1, data.Msg);
      //     }
      //   })
      this.Getcode();//获取手机验证码
    }


  }
  verifyCodeWait = true;
  vcodetime = 60;
  countDownTimer;
  countDown() {
    this.verifyCodeWait = false;
    this.countDownTimer = setInterval(() => {
      this.vcodetime--;
      if (this.vcodetime === 0) {
        this.verifyCodeWait = true;
        this.vcodetime = 60;
        clearInterval(this.countDownTimer);
      }
    }, 1000)
  }

  resetPassword() {
    if (this.mobile == null || this.mobile == "") {
      this.api.showNoticeByToast(1, "请填手机号");
      return false;
    }
    if (this.password!=this.password2) {
      this.api.showNoticeByToast(1, "两次密码不正确");
      return false;
    } 
    if (this.vcode == null||this.vcode=="") {
      this.api.showNoticeByToast(1, "请填写验证码");
      return false;
    }

    let loading = this.loadingCtrl.create({
      content: '请稍候...'
    });
    loading.present();

    this.api.postBody("V1/RsetPassword", {
      "PhoneNumber": this.mobile,
      "Password": this.password,
      "Code": this.vcode
    })
      .then((data) => {
        if (data.Code == 0) {
          this.navCtrl.push(LoginPage);
        }else{
          this.api.showNoticeByToast(1, data.Msg);
        }
        loading.dismiss();
      })
      .catch((error)=>{
        this.api.showNoticeByToast(1, error.Msg);
      })
  }

  showTip(msg) {
    if (typeof msg === 'string') {
      let toast = this.toastCtrl.create({
        message: msg,
        position: 'top',
        duration: 3000
      });
      toast.present();
    }
  }
  chakanPwd = true;
  chakanPwd2 = true;

  chaPwd(key) {
    if (key == 1) {
      this.chakanPwd = !this.chakanPwd;
    } else {
      this.chakanPwd2 = !this.chakanPwd2;
    }
  }
  /**
 * 获取手机验证码
 */
  Getcode() {
    this.verifyCodeWait = true;
    this.countDown();
    this.url.getVcode(this.mobile, 2)
      .then(data => {
        if (data.Code == 0) {
          // this.api.showNoticeByToast(1, data.Msg);
        } else {
          this.api.showNoticeByToast(1, data.Msg);
        }
      })
      .catch(error => {

      })
  }
}
